package com.zhuojy.equity.mall.admin.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuojy.equity.mall.admin.common.entity.TUser;
import com.zhuojy.equity.mall.admin.common.support.Result;
import com.zhuojy.equity.mall.admin.core.LoginCore;
import com.zhuojy.equity.mall.admin.core.QueryUserInfoCore;
import com.zhuojy.equity.mall.admin.core.RegisterCore;
import com.zhuojy.equity.mall.admin.dal.dataobject.TUserDO;
import com.zhuojy.equity.mall.admin.dal.mapper.TUserMapper;
import com.zhuojy.equity.mall.admin.manager.UserManager;
import com.zhuojy.equity.mall.admin.manager.bo.AdminUserDetails;
import com.zhuojy.equity.mall.admin.manager.bo.LoginBO;
import com.zhuojy.equity.mall.admin.manager.bo.RegisterBO;
import com.zhuojy.equity.mall.admin.service.dto.req.*;
import com.zhuojy.equity.mall.admin.service.dto.res.*;
import com.zhuojy.equity.mall.admin.service.struct.LoginDtoStruct;
import com.zhuojy.equity.mall.admin.service.struct.RegisterDtoStruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.security.Principal;

/**
 * 后台用户管理Service实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserService extends ServiceImpl<TUserMapper,TUser> {

	private final RegisterCore registerCore;

	private final LoginCore loginCore;

	private final QueryUserInfoCore queryUserInfoCore;

	private final RegisterDtoStruct registerDtoStruct;

	private final LoginDtoStruct loginDtoStruct;
	private final UserManager userManager;

	/**
	 * 用户注册
	 */
	public Result<RegisterResDTO> register(RegisterReqDTO registerReqDTO) {
		RegisterBO registerBO = registerDtoStruct.toBO(registerReqDTO);
		// 检查用户是否存在
		registerCore.checkUserExisted(registerBO);
		// 用户注册
		registerCore.registerUser(registerBO);
		RegisterResDTO registerResDTO = registerDtoStruct.toResDTO(registerBO);
		return new Result<>(registerResDTO);
	}

	/**
	 * 用户登录
	 */
	public Result<LoginResDTO> login(LoginReqDTO loginReqDTO) {
		LoginBO loginBO = loginDtoStruct.toBO(loginReqDTO);
		loginCore.validateCaptcha(loginBO);
		AdminUserDetails adminUserDetails = loginCore.loadUserByUsername(loginBO.getUsername());
		loginBO.setAdminUserDetails(adminUserDetails);
		loginCore.usernamePasswordAuthentication(loginBO);
		LoginResDTO loginResDTO = loginDtoStruct.toResDTO(loginBO);
		TUserDO tUserDO = userManager.queryUserByUsername(loginBO.getUsername());
		if (tUserDO != null){
			loginResDTO.setTUserDO(tUserDO);
		}
		return new Result<>(loginResDTO);
	}

	public Result<QueryUserInfoResDTO> queryUserInfo(Principal principal) {
		String name = principal.getName();

		return null;
	}

	public Result<QueryUserInfoByIdResDTO> queryUserInfoById(QueryUserInfoByIdReqDTO queryUserInfoByIdReqDTO) {
		return null;
	}

	public Result<ModifyUserInfoResDTO> modifyUserInfo(ModifyUserInfoReqDTO modifyUserInfoReqDTO) {
		return null;
	}

	public Result<ModifyPasswordResDTO> modifyPassword(ModifyPasswordReqDTO modifyPasswordReqDTO) {
		return null;
	}

	public Result<DeleteUserResDTO> deleteUser(DeleteUserReqDTO deleteUserReqDTO) {
		return null;
	}

	public Result<AllotRoleResDTO> allotRole(AllotRoleReqDTO allotRoleReqDTO) {
		return null;
	}

	public Result<QueryUserRolesResDTO> queryUserRoles(QueryUserRolesReqDTO queryUserRolesReqDTO) {
		return null;
	}

    public void getId() {
    }
}
